package cn.dnui_lxx432.dnuifood_16110100432.controller;

import android.content.DialogInterface;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cn.dnui_lxx432.dnuifood_16110100432.Adapter.ShopAdapter;
import cn.dnui_lxx432.dnuifood_16110100432.JavaBean.ShopBean;
import cn.dnui_lxx432.dnuifood_16110100432.controller.HomeFragment;

import cn.dnui_lxx432.dnuifood_16110100432.R;

public class MainActivity extends FragmentActivity implements View.OnClickListener {
    private LinearLayout mTabshouye;
    private LinearLayout mTabcollect;
    private LinearLayout mTabsearch;
    private LinearLayout mTabmy;

    private ImageButton mImgshouye;
    private ImageButton mImgcollect;
    private ImageButton mImgsearch;
    private ImageButton mImgmy;

    private Fragment mTab01;
    private Fragment mTab02;
    private Fragment mTab03;
    private Fragment mTab04;

    private TextView shouye;
    private TextView collect;
    private TextView search;
    private TextView my;


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//软件全屏显示，没有标题
        setContentView(R.layout.activity_main);

        initView();
        initEvent();
        setSelect(0);
    }


    private void initEvent()
    {
        mTabshouye.setOnClickListener(this);
        mTabcollect.setOnClickListener(this);
        mTabsearch.setOnClickListener(this);
        mTabmy.setOnClickListener(this);
    }
    private void initView()
    {
        mTabshouye = (LinearLayout) findViewById(R.id.id_tab_shouye);
        mTabcollect = (LinearLayout) findViewById(R.id.id_tab_collect);
        mTabsearch = (LinearLayout) findViewById(R.id.id_tab_search);
        mTabmy = (LinearLayout) findViewById(R.id.id_tab_my);

        mImgshouye = (ImageButton) findViewById(R.id.id_tab_shouye_img);
        mImgcollect = (ImageButton) findViewById(R.id.id_tab_collect_img);
        mImgsearch = (ImageButton) findViewById(R.id.id_tab_search_img);
        mImgmy = (ImageButton) findViewById(R.id.id_tab_my_img);

        shouye= (TextView) findViewById(R.id.shouye);
        collect= (TextView) findViewById(R.id.collect);
        search= (TextView) findViewById(R.id.search);
        my= (TextView) findViewById(R.id.my);
    }
    private void setSelect(int i)
    {
        FragmentManager fm = getSupportFragmentManager();
        //通过getSupportFragmentManager()来获取FragmentManager对象，同时继承
        //fragmentActivity，这样在activity中才能嵌入fragment。
        FragmentTransaction transaction = fm.beginTransaction();
        //每次在使用FragmentTransaction的时候都需要重新获取，每一个FragmentTransaction只能够commit（）一次。
        //保证一些列Fragment操作的原子性
        //开启一个事务
        hideFragment(transaction);
        switch (i)
        {
            case 0:
                if (mTab01 == null)
                {

                    mTab01 = new HomeFragment();
                    transaction.add(R.id.id_content, mTab01);
                } else
                {
                    transaction.show(mTab01);
                }
                mImgshouye.setImageResource(R.drawable.tab_shouye_press  );
                shouye.setTextColor(Color.parseColor("#1296db"));
                break;
            case 1:
            if (mTab02 == null)
            {
                mTab02 = new CollectFragment();
                transaction.add(R.id.id_content, mTab02);
            } else
            {
                transaction.show(mTab02);
            }
            mImgcollect.setImageResource(R.drawable.tab_find_collect_press);
            collect.setTextColor(Color.parseColor("#1296db"));
                break;
            case 2:
            if (mTab03 == null)
            {
                mTab03 = new SearchFragment();
                transaction.add(R.id.id_content, mTab03);
            } else
            {
                transaction.show(mTab03);
            }
            mImgsearch.setImageResource(R.drawable.tab_search_press);
            search.setTextColor(Color.parseColor("#1296db"));
                break;
            case 3:
            if (mTab04 == null)
            {
                mTab04 = new MeFragment();
                transaction.add(R.id.id_content, mTab04);
            } else
            {
                transaction.show(mTab04);
            }
            mImgmy.setImageResource(R.drawable.tab_my_press);
            my.setTextColor(Color.parseColor("#1296db"));
            break; default:
            break;
        }
        transaction.commit();
    }

      private void hideFragment(FragmentTransaction transaction)
    {
        if (mTab01 != null)
        {
            transaction.hide(mTab01); shouye.setTextColor(Color.parseColor("#8a8a8a"));
        }
        if (mTab02 != null)
        {
            transaction.hide(mTab02); collect.setTextColor(Color.parseColor("#8a8a8a"));
        }
        if (mTab03 != null)
        {
            transaction.hide(mTab03); search.setTextColor(Color.parseColor("#8a8a8a"));
        }
        if (mTab04 != null)
        {
            transaction.hide(mTab04); my.setTextColor(Color.parseColor("#8a8a8a"));
        }
    }
    @Override
    public void onClick(View v)
    {
        resetImgs(); switch (v.getId())
    {
        case R.id.id_tab_shouye: setSelect(0);
            break;
        case R.id.id_tab_collect: setSelect(1);
            break;
        case R.id.id_tab_search: setSelect(2); break;
        case R.id.id_tab_my: setSelect(3); break;
        default:
            break;
    }
    }
    @Override
    protected void onResume() { super.onResume();
    }
    private void resetImgs()
    {
        mImgshouye.setImageResource(R.drawable.tab_shouye_normal);//setImageResource图片正常显示
        mImgcollect.setImageResource(R.drawable.tab_find_collect_normal);
        mImgsearch.setImageResource(R.drawable.tab_search_normal);
        mImgmy.setImageResource(R.drawable.tab_my_normal);
    }
}

